package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.entity.DishFlavor;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.mapper.*;
import com.sky.result.PageResult;
import com.sky.service.DishService;
import com.sky.vo.DishVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Slf4j
@Service
public class DishServiceImpl implements DishService {
    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private DishFlovrMapper dishFlovrMapper;
    @Autowired
    private SetmealDishMapper setmealDishMapper;

    @Transactional //开启事务，涉及到多张表的增删改查
    @Override
    public void addDish(DishDTO dishDTO) {
        //1.构造菜品基本信息数据，将其存入到dish表中
        Dish dish = new Dish();
        //属性赋值
        BeanUtils.copyProperties(dishDTO, dish);
        //调用mapper方法
        dishMapper.add(dish);
        log.info("dis.getId：", dish.getId());

        //2.构造菜品口味列表数据，将其存入dish_flavor表中
        List<DishFlavor> dishFlavorList = dishDTO.getFlavors();
        //关联菜品id
        if (dishFlavorList != null && dishFlavorList.size() > 0) {
            dishFlavorList.forEach(flovr -> {
                flovr.setDishId(dish.getId());
            });

            dishFlovrMapper.insertDish(dishFlavorList);
        }
    }
    /**
     * 分页查询
     *
     * @param dishPageQueryDTO
     * @return
     */
    @Override
    public PageResult selectDishPage(DishPageQueryDTO dishPageQueryDTO) {
         PageHelper.startPage(dishPageQueryDTO.getPage(),dishPageQueryDTO.getPageSize());
    Page<DishVO>  voPage=  dishMapper.select(dishPageQueryDTO);
         Page p =(Page) voPage;

        return new PageResult(p.getTotal(), p.getResult());
    }

    @Transactional
    @Override
    public void delete(List<Long> ids) {
   //判断菜品是否起售
        ids.forEach(id ->{
       Dish dish=  dishMapper.selectById(id);
       if (dish.getStatus()== StatusConstant.ENABLE){
           throw new  DeletionNotAllowedException(MessageConstant.DISH_ON_SALE);
       }
        });
   //判断菜品是否被套餐关联
  int count = setmealDishMapper.countByDishId(ids);
  if (count>0){
      throw new DeletionNotAllowedException(MessageConstant.DISH_BE_RELATED_BY_SETMEAL);
  }
   //删除菜品信息，dish表
   dishMapper.deleteDish(ids);
   //删除菜品口味信息,dish_flavor表
       dishFlovrMapper.delete(ids);
    }

    @Override
    public DishVO getById(Long id) {
        //先查询菜品表，且封装到Dishvo表中
        DishVO dishVO = new DishVO();
        Dish dish=dishMapper.selectById(id);

        //属性赋值
        BeanUtils.copyProperties(dish,dishVO);

        //查询菜品口味表
      List<DishFlavor> list= dishFlovrMapper.slecetById(id);
      dishVO.setFlavors(list);

      return dishVO;
    }

    @Transactional
    @Override
    public void update(DishDTO dishDTO) {
//先修改菜品信息
        Dish dish = new Dish();
        BeanUtils.copyProperties(dishDTO,dish);

        dishMapper.updateDish(dish);

//修改口味菜品信息时可能会遇到增删改的操作所以要先全部删除
        dishFlovrMapper.deleteByDishId(dishDTO.getId());
        List<DishFlavor> flavors = dishDTO.getFlavors();
if (flavors !=null && flavors.size()>0){
    //关联菜品id
    flavors.forEach(flavor ->{
        flavor.setDishId(dish.getId());
    });
    dishFlovrMapper.insertDish1(flavors);
}
    }

    @Override
    public void stop(Long id, Integer status) {
        Dish dish = Dish.builder()
                .id(id)
                .status(status)
                .build();
        dishMapper.updateStatus(dish);
    }

    @Override
    public List<Dish> select(Long categoryId) {
    List<Dish> dishes=   dishMapper.selectByCategoryId(categoryId);
        return dishes;
    }

    @Override
    public List<DishVO> listDisVo(Dish dish) {
List<Dish>  list =  dishMapper.selectList(dish);
        List<DishVO> dishVOList = new ArrayList<>();
        for (Dish dishVO : list) {
            DishVO dishVO1 = new DishVO();
            BeanUtils.copyProperties(dishVO,dishVO1);
            //根据菜品id查询对应的口味
            List<DishFlavor> flavors = dishFlovrMapper.slecetById(dishVO.getId());
            dishVO1.setFlavors(flavors);
           dishVOList.add(dishVO1);
        }
        return dishVOList;
    }
}
